package com.hit.sys.modular.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.core.enums.CommonStatusEnum;
import com.hit.core.enums.YesOrNotEnum;
import com.hit.core.exception.ServiceException;
import com.hit.core.factory.PageFactory;
import com.hit.core.pojo.page.PageResult;
import com.hit.sys.modular.app.entity.SysApp;
import com.hit.sys.modular.app.enums.SysAppExceptionEnum;
import com.hit.sys.modular.app.mapper.SysAppMapper;
import com.hit.sys.modular.app.param.SysAppParam;
import com.hit.sys.modular.app.service.SysAppService;
import org.springframework.stereotype.Service;

@Service
public class SysAppServiceImpl extends ServiceImpl<SysAppMapper, SysApp> implements SysAppService {

    @Override
    public PageResult<SysApp> page(SysAppParam sysAppParam) {
        LambdaQueryWrapper<SysApp> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(sysAppParam)) {
            //根据名称模糊查询
            if (ObjectUtil.isNotEmpty(sysAppParam.getName())) {
                queryWrapper.like(SysApp::getName, sysAppParam.getName());
            }
            //根据编码模糊查询
            if (ObjectUtil.isNotEmpty(sysAppParam.getCode())) {
                queryWrapper.like(SysApp::getCode, sysAppParam.getCode());
            }
        }
        queryWrapper.eq(SysApp::getStatus, CommonStatusEnum.ENABLE.getCode());
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public void add(SysAppParam sysAppParam) {
        checkParam(sysAppParam, false);
        SysApp sysApp = new SysApp();
        BeanUtil.copyProperties(sysAppParam, sysApp);
        sysApp.setStatus(CommonStatusEnum.ENABLE.getCode());
        this.save(sysApp);
    }

    @Override
    public void delete(SysAppParam sysAppParam) {
        SysApp sysApp = this.getEntity(sysAppParam);
        sysApp.setStatus(CommonStatusEnum.DELETED.getCode());
        this.updateById(sysApp);
    }

    @Override
    public void edit(SysAppParam sysAppParam) {
        checkParam(sysAppParam, true);
        SysApp sysApp = this.getEntity(sysAppParam);
        BeanUtil.copyProperties(sysAppParam, sysApp);
        sysApp.setStatus(null);
        this.updateById(sysApp);
    }

    @Override
    public SysApp detail(SysAppParam sysAppParam) {
        return this.getEntity(sysAppParam);
    }

    private void checkParam(SysAppParam sysAppParam, boolean isExcludeSelf) {
        Long id = sysAppParam.getId();
        String name = sysAppParam.getName();
        String code = sysAppParam.getCode();
//        String active = sysAppParam.getActive();

        // 查询名称有无重复
        LambdaQueryWrapper<SysApp> appQueryWrapperByName = new LambdaQueryWrapper<>();
        appQueryWrapperByName.eq(SysApp::getName, name)
                .ne(SysApp::getStatus, CommonStatusEnum.DELETED.getCode());

        // 查询编码有无重复
        LambdaQueryWrapper<SysApp> appQueryWrapperByCode = new LambdaQueryWrapper<>();
        appQueryWrapperByCode.eq(SysApp::getCode, code)
                .ne(SysApp::getStatus, CommonStatusEnum.DELETED.getCode());

        // 查询激活状态有无已经有Y的，也就是激活的
//        LambdaQueryWrapper<SysApp> appQueryWrapperByActive = new LambdaQueryWrapper<>();
//        appQueryWrapperByActive.eq(SysApp::getActive, active)
//                .ne(SysApp::getStatus, CommonStatusEnum.DELETED.getCode());

        if (isExcludeSelf) {
            appQueryWrapperByName.ne(SysApp::getId, id);
            appQueryWrapperByCode.ne(SysApp::getId, id);
//            appQueryWrapperByActive.ne(SysApp::getId, id);
        }
        int countByName = this.count(appQueryWrapperByName);
        int countByCode = this.count(appQueryWrapperByCode);
//        int countByActive = this.count(appQueryWrapperByActive);

        if (countByName >= 1) {
            throw new ServiceException(SysAppExceptionEnum.APP_NAME_REPEAT);
        }
        if (countByCode >= 1) {
            throw new ServiceException(SysAppExceptionEnum.APP_CODE_REPEAT);
        }

        // 只判断激活状态为Y时候数量是否大于1了
//        if (countByActive >= 1 && YesOrNotEnum.Y.getCode().equals(sysAppParam.getActive())) {
//            throw new ServiceException(SysAppExceptionEnum.APP_ACTIVE_REPEAT);
//        }
    }

    private SysApp getEntity(SysAppParam sysAppParam) {
        SysApp entity = this.getById(sysAppParam.getId());
        if (ObjectUtil.isNull(entity)) {
            throw new ServiceException(SysAppExceptionEnum.APP_NOT_EXIST);
        }
        return entity;
    }

}
